<?php
/**
 * Installer class
 *
 *
 * @copyright		The XOOPS project http://www.xoops.org/
 * @license			http://www.fsf.org/copyleft/gpl.html GNU General Public License (GPL)
 * @package			installer
 * @since			2.0.18
 * @version			$Id: installwizard.php 604 2006-07-01 22:28:37Z skalpa $
 *
 * @author			Haruki Setoyama  <haruki@planewave.org>
 * @author			Kazumi Ono <webmaster@myweb.ne.jp>
 * @author			Skalpa Keo <skalpa@xoops.org>
 * @author			dugris <dugris@frxoops.org>
**/

if ( !defined('XOOPS_INSTALL') ) { die('XOOPS Custom Installation die'); }

class XoopsInstallWizard {

	var $pages = array();
	var $titles = array();
	var $currentPage = 0;
	var $language = 'english';
	var $actionForm = '';
	var $actionName = '';

	function xoInit() {
		if ( @empty( $_SERVER['REQUEST_URI'] ) ) {
			$_SERVER['REQUEST_URI'] = $_SERVER['PHP_SELF'];
		}
		$this->actionForm = $_SERVER['PHP_SELF'];

		// Load the main language file
		$this->initLanguage( !@empty( $_COOKIE['xo_install_lang'] ) ? $_COOKIE['xo_install_lang'] : 'english' );

		if ( !$this->checkAccess() ) {
			return false;
		}

		// Setup pages
		$this->pages		= array(
			'langselect', 'start', 'modcheck',
			'pathsettings', 'dbsettings', 'configsave',
			'tablescreate', 'siteinit', 'tablesfill',
			'configsite', 'moduleinstaller', 'theme',
			'end',
		);
		$this->pagesNames	= array(
			LANGUAGE_SELECTION, INTRODUCTION, CONFIGURATION_CHECK,
			PATHS_SETTINGS, DATABASE_CONFIG, CONFIG_SAVE,
			TABLES_CREATION, INITIAL_SETTINGS, DATA_INSERTION,
			CONFIG_SITE, MODULES, THEME,
			WELCOME,
		);
		$this->pagesTitles	= array(
			LANGUAGE_SELECTION_TITLE, INTRODUCTION_TITLE, CONFIGURATION_CHECK_TITLE,
			PATHS_SETTINGS_TITLE, DATABASE_CONFIG_TITLE, CONFIG_SAVE_TITLE,
			TABLES_CREATION_TITLE, INITIAL_SETTINGS_TITLE, DATA_INSERTION_TITLE,
			CONFIG_SITE_TITLE, MODULES_TITLE, THEME_TITLE,
			WELCOME_TITLE,
		);

		include_once './include/config.php';

		// Database type
		$this->db_types  = $db_types;

		// setup config site info
		$this->conf_names  = $conf_names;

		// languages config files
		$this->language_files = $language_files;

		// extension_loaded
		$this->extensions = $extensions;

		// Modules to be installed by default
		$this->modules = $modules;

		// Protector default trust_path
		$this->trust_path = isset($trust_path) ? $trust_path : false;

		// Writable files and directories
		$this->paths = $paths;
		if ( $this->trust_path ) {
			array_push ($this->paths, $this->trust_path . '/modules/protector/configs/');
		}

		$this->setPage(0);
		// Prevent client caching
		header( "Cache-Control: no-store, no-cache, must-revalidate", false );
		header( "Pragma: no-cache" );
		return true;
	}

	function checkAccess() {
		if ( $_SERVER['REQUEST_METHOD'] && $_SERVER['REQUEST_METHOD'] == 'POST' ) {
			if ( isset($_POST['uname']) && isset($_POST['pass']) ) {
				include 'include/checklogin.php';
			}
		}

		if ( defined( 'XOOPS_MAINFILE_INCLUDED') && ( !isset($_SESSION['Login']) || !$_SESSION['Login']) ) {
			include 'include/login.php';
		}

		/*
		if ( INSTALL_USER != '' && INSTALL_PASSWORD != '' ) {
			if (!isset($_SERVER['PHP_AUTH_USER']) ) {
				header('WWW-Authenticate: Basic realm="XOOPS Installer"');
				header('HTTP/1.0 401 Unauthorized');
				echo 'You can not access this XOOPS installer.';
				return false;
			}
			if( INSTALL_USER != '' && $_SERVER['PHP_AUTH_USER'] != INSTALL_USER) {
				header('HTTP/1.0 401 Unauthorized');
				echo 'You can not access this XOOPS installer.';
				return false;
			}
			if( INSTALL_PASSWD != $_SERVER['PHP_AUTH_PW'] ){
				header('HTTP/1.0 401 Unauthorized');
				echo 'You can not access this XOOPS installer.';
				return false;
			}
		}
		*/
		return true;
	}

	function loadLangFile( $file ) {
		if ( file_exists( "./language/$this->language/$file.php" ) ) {
			include_once "./language/$this->language/$file.php";
		} else {
			include_once "./language/english/$file.php";
		}
	}

	function initLanguage( $language ) {
		if ( !file_exists( "./language/$language/install.php" ) ) {
			$language = 'english';
		}
    	$this->language = $language;
		$this->loadLangFile( 'install' );
	}

	function setPage( $page ) {
		if ( (int)$page && $page >= 0 && $page < count($this->pages) ) {
			$this->currentPageName = $this->pages[ $page ];
			$this->currentPage = $page;
		} elseif ( false !== ( $index = array_search( $page, $this->pages ) ) ) {
			$this->currentPageName = $page;
			$this->currentPage = $index;
		} else {
			return false;
		}

		if ( $this->currentPage > 0 && count($_SESSION['settings']) == 0 ) {
			header('Location: index.php');
		}

		return $this->currentPage;
	}

	function baseLocation() {
		$proto	= ( @$_SERVER['HTTPS'] == 'on') ? 'https' : 'http';
		$host	= $_SERVER['HTTP_HOST'];
		$base	= substr( $_SERVER['PHP_SELF'], 0, strrpos( $_SERVER['PHP_SELF'], '/' ) );
		return "$proto://$host$base";
	}

	function pageURI( $page ) {
		if ( !(int)$page{0} ) {
			if ( $page{0} == '+' ) {
				$page = $this->currentPage + substr( $page, 1 );
			} elseif ( $page{0} == '-' ) {
				$page = $this->currentPage - substr( $page, 1 );
			} else {
				$page = (int)array_search( $page, $this->pages );
			}
		}
		$page = $this->pages[$page ];
		return $this->baseLocation() . "/page_$page.php";
	}

	function redirectToPage( $page, $status = 303, $message = 'See other' ) {
		$location = $this->pageURI( $page );
		$proto = !@empty($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
		header( "$proto $status $message" );
		//header( "Status: $status $message" );
		header( "Location: $location" );
	}

	function ext_loaded() {
		foreach ( $this->extensions as $ext => $value) {
			if ( extension_loaded( $ext ) ) {
				if ( is_array($value[0] ) ) {
					$this->extensions[$ext][] = xoDiag( 1, implode( ',', $value[0] ) );
				} else {
					$this->extensions[$ext][] = xoDiag( 1, $value[0] );
				}
			} else {
				$this->extensions[$ext][] = xoDiag( 0, NONE );
			}
		}
	}

	function loadConfigLangFile() {
		if ( file_exists( "../modules/system/language/$this->language/admin.php" ) ) {
			include_once "../modules/system/language/$this->language/admin.php";
		} else {
			include_once "../modules/system/language/english/admin.php";
		}

		if ( file_exists( "../modules/system/language/$this->language/admin/preferences.php" ) ) {
			include_once "../modules/system/language/$this->language/admin/preferences.php";
		} else {
			include_once "../modules/system/language/english/admin/preferences.php";
		}
		foreach( $this->language_files as $file) {
			if ( file_exists( "../language/$this->language/$file.php" ) ) {
				include_once "../language/$this->language/$file.php";
			} else {
				include_once "../language/english/$file.php";
			}
		}
	}

	function loadModuleLangFile() {
		if ( file_exists( "../language/$this->language/global.php" ) ) {
			include_once "../language/$this->language/global.php";
		} else {
			include_once "../language/english/global.php";
		}

		if ( file_exists( "../modules/system/language/$this->language/admin/modulesadmin.php" ) ) {
			include_once "../modules/system/language/$this->language/admin/modulesadmin.php";
		} else {
			include_once "../modules/system/language/english/admin/modulesadmin.php";
		}
	}

	function loadConfigInfo() {
		$config_handler = xoops_gethandler('config');
		$criteria = new CriteriaCompo();
		$criteria->add(new Criteria('conf_modid', 0));

		$criteria2 = new CriteriaCompo();
		foreach ($this->conf_names as $conf_name) {
   		$criteria2->add(new Criteria('conf_name', $conf_name), 'OR');
		}
		$criteria->add($criteria2);
		$criteria->setSort('conf_catid ASC, conf_order ASC');
		$this->config = $config_handler->getConfigs($criteria);

		include './include/createconfigform.php';
		$this->form = createConfigform($this->config);
	}

	function loadThemeInfo() {
		$config_handler = xoops_gethandler('config');
		$criteria = new CriteriaCompo();
		$criteria->add(new Criteria('conf_modid', 0));
		$criteria->add(new Criteria('conf_name', 'theme_set'));

		$this->config = $config_handler->getConfigs($criteria);
		include './include/createconfigform.php';
		$this->form = createThemeform($this->config[0]);
	}

	function CreateForm() {
		$hidden = '';
		$ret = '';

		foreach ($this->form as $form) {
			$ret .= "<fieldset><legend>" . $form->getTitle() . "</legend>\n";

			foreach( $form->getElements() as $ele) {
				if (is_object($ele)) {
					if ( !$ele->isHidden() ) {
						if ( ($caption = $ele->getCaption()) != '' ) {
							$name = $ele->getName();
							$ret .= "<label for='" . $ele->getName() . "'>" . $caption ;
							if ( ($desc = $ele->getDescription()) != '' ) {
								$ret .= "<img class='Tips_$name' id='help_button' src='img/contexthelp.png' title='" . htmlspecialchars($caption, ENT_QUOTES ) . ' :: ' . htmlspecialchars($desc, ENT_QUOTES ) . "' />";
								$GLOBALS['wizard']->helper[] = "Tips_$name";
							}
							$ret .= "</label>\n";
						}
						$ret .= $ele->render() . "\n";
					} else {
						$hidden .= $ele->render() . "\n";
					}
				}
			}
			$ret .= "</fieldset>\n" . $hidden. "\n" . $form->renderValidationJS( true ) ;
		}
		return $ret;
	}
}
?>